package gs.android.calcfeud;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.graphics.Typeface;

public class Button extends Sprite {
	private Paint paint;
	private Rect bounds, dst;
	
	private String text;
	private boolean enabled, round;
	
	public Button(float x, float y, float height, float width, boolean round, String text, Bitmap bitmap) {
		super(x, y, height, width, bitmap);
		this.enabled = false;
		this.round = round;
		this.text = text;
		
		bounds = new Rect();
		dst = new Rect((int)x, (int)y, (int)(x + getWidth()), (int)(y + getHeight()));
		
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setTextSize(20 * GamePersistor.SCALE);
		paint.setTextAlign(Align.CENTER);
		paint.setTypeface(Typeface.SERIF);
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void onDraw(Canvas c, int delta) {
		c.drawBitmap(getBitmap(), null, dst, paint);
		
		if (!text.equals("")) {
			paint.getTextBounds(text, 0, text.length(), bounds);
		    int newY = (int)((getHeight() + bounds.height()) / 2);
			int newX = (int)(getX() + (getWidth() / 2));
			
			c.drawText(text, newX, getY() + newY, paint);
		}
	}
	
	public boolean isPressed(float x, float y) {
		if (round) {
			double a = Math.pow((getX() + (getWidth() / 2)) - x, 2);
			double b = Math.pow((getY() + (getHeight() / 2)) - y, 2);
			double c = Math.sqrt(a + b);
			
			if (c < getWidth() / 2)
				return true;
			else
				return false;
		} else {
			if (getX() <= x && getX() + getWidth() >= x && getY() <= y && getY() + getHeight() >= y)
				return true;
			else
				return false;
		}
	}
}